package com.chance.cc.crawler.prod.command.job.domain.commerce.tb.supplement;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.CrawlerEnum;
import com.chance.cc.crawler.core.CrawlerJob;
import com.chance.cc.crawler.core.downloader.HttpConstant;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.core.downloader.HttpRequestBody;
import com.chance.cc.crawler.core.filter.FilterUtils;
import com.chance.cc.crawler.core.record.CrawlerRequestRecord;
import com.chance.cc.crawler.meta.core.bean.CrawlerMetaConstant;
import com.chance.cc.crawler.meta.core.bean.job.CrawlerScheduleJob;
import com.chance.cc.crawler.prod.command.job.domain.commerce.tb.TBCommonCrawlerSchedulerJob;
import org.apache.commons.lang3.StringUtils;

import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerDataType.comment;
import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerRequestType.*;

/**
 * created by CC on 2020/11/26
 * mail 279020185@qq.com
 */
public class TBProductCommentSupplementCrawlerScheduleJob extends TBCommonCrawlerSchedulerJob {

    private static String searchUrl = "https://rate.taobao.com/feedRateList.htm";


    public static void main(String[] args) {
        publishTBCommentSchedulerJob();
    }

    public static CrawlerJob publishTBCommentSchedulerJob(){
        CrawlerJob taobaoSchduler = taobaoSchduler();

        //发布定时采集作业
        CrawlerScheduleJob crawlerScheduleJob = new CrawlerScheduleJob();
        crawlerScheduleJob.setDomain(domain);
        crawlerScheduleJob.setCrawlerJob(JSON.toJSONString(taobaoSchduler));
        crawlerScheduleJob.setJobType(CrawlerMetaConstant.ScheduleCrawlerJobType.crawler.enumVal());
        crawlerScheduleJob.setNote("淘宝评论单次补采");
        crawlerScheduleJob.setCrawlerKey(taobaoSchduler.generateCrawlerKey());
        HttpPage page = metaServiceCommand.addOrUpdateCrawlerScheduleJob(crawlerScheduleJob);
        System.out.println("发布作业：" + page.getRawText());

        return taobaoSchduler;
    }

    /**
     * taobao采集
     * @return
     */
    public static CrawlerJob taobaoSchduler() {

        long[] dateRange = new long[2];
        dateRange[0] = 1569686400000L; //20190929
        dateRange[1] = System.currentTimeMillis();

        CrawlerRequestRecord requestRecord = CrawlerRequestRecord.builder()
                .startPageRequest(domain, CrawlerEnum.CrawlerRequestType.turnPage)
                .httpUrl(searchUrl)
                .releaseTime(System.currentTimeMillis())
                .needWashed(false)
                .needParsed(false)
                .resultLabelTag(comment)
                .filter(CrawlerEnum.CrawlerRecordFilter.dateRange)
                .addFilterInfo(FilterUtils.dateRangeFilterInfo(-1,dateRange))
                .proxy(proxy)
                .build();
        requestRecord.setDownload(false);
        requestRecord.setSkipPipeline(true);

        String triggerJobKey = StringUtils.joinWith("-","comment","supplement",CrawlerMetaConstant.ScheduleJobTriggerJob_Realtime);
        String triggerKey = CrawlerMetaConstant.ScheduleJobTrigger_Cron;

        //初始 comment crawler request record
        String requestQueueName = StringUtils.joinWith("-","crawler",domain,"comment","supplement","queue");
        CrawlerRequestRecord initCommentCrawlerRecord = CrawlerRequestRecord.builder()
                .startPageRequest("taobao_keywords",turnPageItem)
                .httpUrl(metaServiceHttpPrefix + "/crawler/domain/common/api/v1/taobao/results/queue/init?requestQueueName="+requestQueueName+"&site=supplement")
                .requestLabelTag(supportSource)
                .requestLabelTag(internalDownload)
                .build();
        HttpRequestBody jsonBody = HttpRequestBody.json(JSON.toJSONString(requestRecord), "utf-8");
        initCommentCrawlerRecord.getHttpRequest().setMethod(HttpConstant.Method.POST);
        initCommentCrawlerRecord.getHttpRequest().setRequestBody(jsonBody);

        //user agents source record
        CrawlerRequestRecord userAgentsCrawlerRecord = CrawlerRequestRecord.builder()
                .startPageRequest("taobao_comment_user_agents", turnPageItem)
                .httpUrl(userAgentUrl)
                .requestLabelTag(supportSource)
                .requestLabelTag(internalDownload)
                .build();

        return CrawlerJob.builder()
                .crawlerJobThreadNumber(12)
                .triggerInfo(
                        domain,
                        triggerKey,
                        System.currentTimeMillis(),
                        triggerJobKey)
                .crawlerRequestQueue(CrawlerMetaConstant.redisRequestQueue(requestQueueName))
                .kafkaResultPipeline(null,kafkaTopic_supplement,null)
                .requestRecord(requestRecord)
                .supportRecord(userAgentsCrawlerRecord)
                .supportRecord(initCommentCrawlerRecord)
                .build();
    }

}
